home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / threads / example / PhilAnimator.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.7 KB  |  79 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17.  
  18. import java.awt.*;
  19.  
  20. public class PhilAnimator extends java.applet.Applet {
  21.  
  22.     Button stopStartButton = new Button("start");
  23.  
  24.         // delays can go from 500 to 10,000 (they get multiplied by 100 in Philosopher
  25.     Scrollbar grabDelaySlider = new Scrollbar(Scrollbar.HORIZONTAL, 5, 100, 0, 100);
  26.     Label label = new Label("  500 milliseconds");
  27.  
  28.     FramedArea framedArea;
  29.  
  30.     public void init() {
  31.         GridBagLayout gridBag = new GridBagLayout();
  32.         GridBagConstraints c = new GridBagConstraints();
  33.  
  34.         setLayout(gridBag);
  35.  
  36.         framedArea = new FramedArea(this);
  37.         c.fill = GridBagConstraints.BOTH;
  38.         c.weighty = 1.0;
  39.         c.gridwidth = GridBagConstraints.REMAINDER; //end row
  40.         gridBag.setConstraints(framedArea, c);
  41.         add(framedArea);
  42.  
  43.         c.fill = GridBagConstraints.HORIZONTAL;
  44.         c.weightx = 1.0;
  45.         c.weighty = 0.0;
  46.         gridBag.setConstraints(stopStartButton, c);
  47.         add(stopStartButton);
  48.  
  49.  
  50.         c.gridwidth = GridBagConstraints.RELATIVE; //don't end row
  51.         c.weightx = 1.0;
  52.         c.weighty = 0.0;
  53.         gridBag.setConstraints(grabDelaySlider, c);
  54.         add(grabDelaySlider);
  55.  
  56.         c.weightx = 0.0;
  57.         c.gridwidth = GridBagConstraints.REMAINDER; //end row
  58.         gridBag.setConstraints(label, c);
  59.         add(label);
  60.  
  61.         validate();
  62.     }
  63.  
  64.     public boolean handleEvent(Event e) {
  65.         if (e.target == stopStartButton) {
  66.             if (stopStartButton.getLabel().equals("stop/reset")) {
  67.                 framedArea.stopButton();
  68.                 stopStartButton.setLabel("start");
  69.             } else if (stopStartButton.getLabel().equals("start")) {
  70.                 framedArea.startButton();
  71.                 stopStartButton.setLabel("stop/reset");
  72.             }
  73.         } else if (e.target == grabDelaySlider) {
  74.             label.setText(String.valueOf(100*grabDelaySlider.getValue()) + " milliseconds");
  75.         }
  76.         return false;
  77.     }
  78. }
  79.